package com.txr.news.controller;

import com.txr.news.pojo.Code;
import com.txr.news.pojo.Result;
import com.txr.news.pojo.entity.User;
import com.txr.news.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@CrossOrigin  //解决跨域
@AllArgsConstructor
public class AuthController {
    UserService userService;

    @PostMapping("/reguser")
    public Result register(User user){
        Result result = new Result(0,"注册成功！",null, Code.RegisterOk);
        Boolean flag = userService.registerUser(user);
        if (!flag){
            //用户存在，注册失败
            result.setStatus(1);
            result.setMsg("用户已存在，注册失败！");
        }
        return result;
    }

    @PostMapping("/login")
    public Result login(User user){
        //Result result = new Result(0,"登录成功！");
        Result ret = userService.loginUser(user);
        return ret;
    }
}
